package sing.core.ext.view

import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2

fun ViewPager.onPageSelected(onPageSelected: (Int) -> Unit) {

    this.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        }

        override fun onPageSelected(position: Int) {
            onPageSelected.invoke(position)
        }

        override fun onPageScrollStateChanged(state: Int) {
        }
    })
}


fun ViewPager.init(views: ArrayList<View>): ViewPager {
    this.offscreenPageLimit = views.size
    adapter = object : PagerAdapter() {
        override fun getCount() = views.size
        override fun isViewFromObject(view: View, obj: Any) = view == obj
        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = views[position]
            container.addView(view)
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
            container.removeView(obj as View)
        }
    }
    return this
}

fun ViewPager.init(fm: FragmentManager, fragments : ArrayList<Fragment> = arrayListOf(), action: (index: Int) -> Unit = {}) {
    adapter = object : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        override fun getCount() = fragments.size
        override fun getItem(position: Int) = fragments[position]
    }

    addOnPageChangeListener(object: ViewPager.OnPageChangeListener {
        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        }

        override fun onPageSelected(position: Int) {
            action.invoke(position)
        }

        override fun onPageScrollStateChanged(state: Int) {
        }
    })
}

fun ViewPager2.init(fragment: FragmentActivity, fragments: ArrayList<Fragment>, isUserInputEnabled: Boolean = true, action: (index: Int) -> Unit = {}): ViewPager2 {
    val view: View = getChildAt(0)
    if (view is RecyclerView) {
        view.overScrollMode = View.OVER_SCROLL_NEVER
    }
    //是否可滑动
    this.isUserInputEnabled = isUserInputEnabled
    //设置适配器
    adapter = object : FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            action.invoke(position)
        }
    })

    return this
}

fun ViewPager2.init(fragment: Fragment, fragments: ArrayList<Fragment>, isUserInputEnabled: Boolean = true, action: (index: Int) -> Unit = {}): ViewPager2 {
    val view: View = getChildAt(0)
    if (view is RecyclerView) {
        view.overScrollMode = View.OVER_SCROLL_NEVER
    }

    this.isUserInputEnabled = isUserInputEnabled
    if (fragments.size > 0){
        this.offscreenPageLimit = fragments.size
    }
    adapter = object : FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            action.invoke(position)
        }
    })
    return this
}
